package com.zidian.leader.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.zidian.leader.R;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.activity.MySwipeBackActivity;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.StudentEvaTwoIndex;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;

/**
 * 学生评价二级标签动态
 * Created by GongCheng on 2017/1/12.
 */

public class StudentEvaTwoIndexActivity extends MySwipeBackActivity {
    @Bind(R.id.student_two_index_line_chart)
    LineChart studentTwoIndexLineChart;

    private String twoIndexName;
    private String collegeName;

    private List<String> month;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        twoIndexName = intent.getStringExtra("twoIndexName");
        collegeName = intent.getStringExtra("collegeName");
        month = new ArrayList<>();
        //初始化toolbar
        actionBar.setTitle(twoIndexName);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp);
        //初始化LineChart
        initLineChart(studentTwoIndexLineChart);
        //获取数据
        getStudentEvaTwoIndex();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_student_eva_two_index;
    }

    private void initLineChart(LineChart chart) {
        chart.setTouchEnabled(false);
        chart.setDescription(null);
        XAxis xAxis = chart.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(5);
        xAxis.setTextSize(10f);

        xAxis.setGranularity(1);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //Y轴左侧
        YAxis yl = chart.getAxisLeft();
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(true);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        //Y轴右侧
        YAxis yr = chart.getAxisRight();
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(false);
        yr.setAxisMinimum(0f);
        yr.setEnabled(false);
        chart.animateX(1000);
        chart.animateY(1500);
        //去掉图表说明
        Legend l = chart.getLegend();
        l.setEnabled(false);
    }

    private LineData getLineData(List<StudentEvaTwoIndex> data) {
        List<Entry> entries = new ArrayList<>();
        entries.add(new Entry(0, 0));
        for (int i = 0; i < data.size(); i++) {
            entries.add(new Entry(i + 1, data.get(i).getTwoIndexScore()));
        }
        LineDataSet dataSet = new LineDataSet(entries, "LineChart");

        dataSet.setCubicIntensity(1f);
        //去掉圆点
        dataSet.setDrawCircles(false);
        //设置模式为贝塞尔曲线
        dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        //是否显示值
        dataSet.setDrawValues(false);
        //设置阴影
        dataSet.setDrawFilled(true);
        dataSet.setFillColor(Color.parseColor("#caedfe"));
        dataSet.setColor(Color.parseColor("#6ea9ff"));
        dataSet.setLineWidth(2);

        return new LineData(dataSet);
    }

    private void getStudentEvaTwoIndex() {
        subscription = HttpFactory.getInstance().getStudentEvaTwoIndex(collegeName,
                twoIndexName, HawkUtils.getUsername(), HawkUtils.getRoleId() +
                        StringConstants.STRING_EMPTY, HawkUtils.getToken())
                .subscribe(new Subscriber<List<StudentEvaTwoIndex>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Snackbar.make(studentTwoIndexLineChart, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<StudentEvaTwoIndex> studentEvaTwoIndices) {
                        XAxis xAxis = studentTwoIndexLineChart.getXAxis();
                        month.add("0");
                        for (int i = 0; i < studentEvaTwoIndices.size(); i++) {
                            month.add(studentEvaTwoIndices.get(i).getTheMonth());
                        }
                        //X轴Value设计
                        xAxis.setValueFormatter(new IAxisValueFormatter() {
                            @Override
                            public String getFormattedValue(float value, AxisBase axis) {
                                if (month.size() < value + 1) {
                                    return "null";
                                }
                                return month.get((int) value);
                            }

                            @Override
                            public int getDecimalDigits() {
                                return 0;
                            }
                        });
                        studentTwoIndexLineChart.setData(getLineData(studentEvaTwoIndices));
                        studentTwoIndexLineChart.invalidate();
                    }
                });
    }
}
